package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.activity.commonwish.CommonWishConfig;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.commonwish.CommonWishMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CommonWishDrawRecycleStrategy extends AbstractItemRecycleStrategy {

    @Override
    public void markNeedRecycleItem() {
        for (Map.Entry<Integer, CommonWishConfig> configEntry : CommonWishMgr.getConfigMap().entrySet()) {
            ActivityInfo activityInfo = configEntry.getValue().getActivityInfo();
            CommonWishConfig config = configEntry.getValue();
            if("0".equals(config.getItemRecycle()) || StringUtils.isNullOrEmpty(config.getItemRecycle())){
                continue;
            }
            //回收时间
            long recycleTime = activityInfo.getEndShowTime() * 1000L + 600 * 1000;

            List<String> recycleItemList = StringUtils.stringToStringList(config.getItemRecycle(), "\\|");
            for (String recycleItemStr : recycleItemList) {
                List<String> itemList = StringUtils.stringToStringList(recycleItemStr, ";");
                long removeTime = activityInfo.getEndShowTime() * 1000L + 600 * 1000;
                if(itemList.size() == 2){
                    //清空道具
                    if("0".equals(itemList.get(1))){
                        List<String> paramList = new ArrayList<>();
                        //参数1：待回收道具
                        paramList.add(itemList.get(0));
                        //参数2：积分道具
                        paramList.add("0");
                        //参数3：1个道具转换多少倍积分，这边 2 指 itemId 远程道具配置表 GoodsInfo paramList 的下标
                        paramList.add("0");
                        //参数4：活动名称
                        paramList.add(activityInfo.getActivityName());
                        //新增一条回收规则
                        ActivityItemRecycleMgr.addRecycleItemData(Integer.parseInt(itemList.get(0)), activityType, 3, removeTime, paramList);

                    }else {
                        Property reward = PropertyHelper.parseStringToProperty(itemList.get(1));
                        if(reward != null && !reward.isNothing()){
                            List<String> paramList = new ArrayList<>();
                            //参数1：待回收道具
                            paramList.add(itemList.get(0));
                            //参数2：目标道具道具
                            paramList.add(itemList.get(1));
                            //参数3：活动名称
                            paramList.add(activityInfo.getActivityName());
                            //新增一条回收规则
                            ActivityItemRecycleMgr.addRecycleItemData(Integer.parseInt(itemList.get(0)), activityType, 2, recycleTime, paramList);
                        }
                    }

                }
            }

        }
    }


    @Override
    protected void initActivityType() {
        this.activityType = eActivityType.CommonWishDraw.getValue();
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {
        if (data.getRecycleType() == 2) {
            //转其他道具
            String language = UserMgr.getUserInfo(userId).getLanguage();
            UserBagItem userBagItem = getUserBagItem(userId, data.getItemId());
            if (userBagItem != null && userBagItem.getNum() > 0) {
                // 转换其他道具
                GoodsInfo goodsInfo = GoodsMgr.getGoodsById(data.getItemId());
                if(goodsInfo != null){
                    long num = userBagItem.getNum();
                    if (removeItem(userBagItem)){
                        Property reward = PropertyHelper.parseStringToProperty(data.getParamList().get(1));
                        reward.rideProperty(num);
                        //  发邮件
                        String removeName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), language);
                        String activityName = ServerLanguageMgr.getContent(data.getParamList().get(2), language);
                        String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.COMMON_RECYCLE_REMOVE_ITEM_TITLE, language), activityName);
                        String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.COMMON_RECYCLE_REMOVE_ITEM_CONTENT,
                                language), removeName, num);
                        MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(reward), mailContent, mailTitle);
                    }
                }
            }
        } else if(data.getRecycleType() == 3){
            //清空
            int itemId = data.getItemId();
            UserBagItem userBagItem = getUserBagItem(userId, itemId);
            if (userBagItem != null && userBagItem.getNum() > 0) {
                removeItem(userBagItem);
            }
        }
    }

    @Override
    public boolean isActivityRunning() {
        return false;
    }
}
